//
//  SelectPhotoPictureGroupViewController.m
//  Rail
//
//  Created by hychen on 2018/3/4.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "SelectPhotoPictureGroupViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "BMPhotoIOAlbumModel.h"
#import "BMDailyPhotoSelectPictureGroupCell.h"

@interface SelectPhotoPictureGroupViewController ()<UITableViewDataSource,UITableViewDelegate>
/***  列表*/
@property (nonatomic, weak) UITableView *tableView;
/***  列表*/
@property (nonatomic, weak) UIView *backView;
/***  当前组*/
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;
@end

@implementation SelectPhotoPictureGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.clipsToBounds = YES;
    self.selectedIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self setupUI];
}

- (void)setupUI
{
    [self setupBackView];
    
    [self setupTableView];
}

- (void)setupBackView
{
    __weak typeof (self) weakSelf = self;
    UIView *backView = [[UIView alloc] init];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidTap:)];
    [backView addGestureRecognizer:tapGesture];
    [self.view addSubview:backView];
    self.backView = backView;
    backView.backgroundColor = Color_RGBA(0, 0, 0, 0.5);
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
}

- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.dataSource = self;
    tableView.delegate = self;
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [tableView setSeparatorColor:[UIColor colorWithRed:232.0/255 green:232.0/255 blue:232.0/255 alpha:1.0]];

    [self.view addSubview:tableView];
    self.tableView = tableView;
    
}

#pragma mark - 属性
- (void)setGroupArray:(NSMutableArray *)groupArray
{
    _groupArray = groupArray;
    if (groupArray) {
        CGFloat height = self.groupArray.count * 60;
        if (height > self.view.frame.size.height - [BMUtility statusBarAndNavBarHeight]) {
            height = self.view.frame.size.height - [BMUtility statusBarAndNavBarHeight];
        }
        [self.tableView setHeight:height];
        [self.tableView reloadData];
    } else {
        groupArray = [NSMutableArray array];
    }
}

- (void)setHidden:(BOOL)hidden
{
    _hidden = hidden;
    self.view.hidden = hidden;
    if (!hidden) {
        self.backView.hidden = hidden;
        CGFloat height = self.groupArray.count * 60;
        if (height > self.view.frame.size.height - [BMUtility statusBarAndNavBarHeight]) {
            height = self.view.frame.size.height - [BMUtility statusBarAndNavBarHeight];
        }
        
        [self.tableView setHeight:height];
    }
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.groupArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BMDailyPhotoSelectPictureGroupCell *cell = [BMDailyPhotoSelectPictureGroupCell cellWithTableView:tableView class:[BMDailyPhotoSelectPictureGroupCell class]];
    
    BMPhotoIOAlbumModel *album = [self.groupArray objectAtIndexSafe:indexPath.row];
    
    cell.albumModel = album;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == self.selectedIndexPath.section && indexPath.row == self.selectedIndexPath.row) {
        [self viewDidTap:nil];
        return;
    }
    // 修改当前选中indexPath
    self.selectedIndexPath = indexPath;
    // 调用代码块通知
    if (self.selectPhotoGroup) {
        self.selectPhotoGroup(@(indexPath.row));
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)viewDidTap:(UITapGestureRecognizer *)gesture
{
    __weak typeof (self) weakSelf = self;
    // 背景view动画
    [UIView animateWithDuration:0.05 animations:^{
        weakSelf.backView.hidden = YES;
    }];
    [UIView animateWithDuration:0.2 animations:^{
        [weakSelf.tableView setHeight:0.0];
    } completion:^(BOOL finished) {
        weakSelf.view.hidden = YES;
        _hidden = YES;
    }];
    
}


@end
